package cn.tedu.property_management_system.service.Impl;

import cn.tedu.property_management_system.cache.repo.IRepairRepository;
import cn.tedu.property_management_system.entity.Complaint;
import cn.tedu.property_management_system.entity.DTO.RepairAddNewDTO;
import cn.tedu.property_management_system.entity.Repair;
import cn.tedu.property_management_system.entity.RepairType;
import cn.tedu.property_management_system.entity.VO.RepairVO;
import cn.tedu.property_management_system.entity.VO.UserVO;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.mapper.RepairMapper;
import cn.tedu.property_management_system.mapper.RepairTypeMapper;
import cn.tedu.property_management_system.mapper.UserMapper;
import cn.tedu.property_management_system.service.IRepairService;
import cn.tedu.property_management_system.utils.JsonResult;
import cn.tedu.property_management_system.utils.SecurityUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import static cn.tedu.property_management_system.utils.Constant.DELETE_FAULT;
import static cn.tedu.property_management_system.utils.Constant.HANDLE_SUCCESS;
import static cn.tedu.property_management_system.utils.JsonResult.State.NO_SUCH_RESOURCE;
import static cn.tedu.property_management_system.utils.JsonResult.State.UNKNOW_EXCEPTION;

@Slf4j
@Service
public class RepairServiceImpl implements IRepairService {

    @Autowired
    IRepairRepository repairRepository;

    @Autowired
    private UserMapper userMapper;

    @Autowired(required = false)
    private RepairMapper repairMapper;

    @Autowired(required = false)
    private RepairTypeMapper repairTypeMapper;


    @Override
    public PageInfo<RepairVO> loadRepair(Integer pageNum,Integer PageSize) {
        log.debug("加载[报修]信息列表");
        if (repairRepository.hasPageKey(pageNum)){
            PageInfo<RepairVO> pageInfo = repairRepository.getPageCache(pageNum);
            log.debug("缓存中存在数据,不再进入数据查询[报修]列表");
            return pageInfo;
        }
        PageHelper.startPage(pageNum,PageSize);
        List<RepairVO> repairVOS = repairMapper.listAllRepairVO();
        PageInfo<RepairVO> pageInfo = new PageInfo<>(repairVOS);
        log.debug("[投诉]列表缓存过期，进入数据库查询数据，查询结果为[{}]",repairVOS);
        if (repairVOS==null){
            log.debug("[报修]列表为空,抛出异常");
            throw new ServiceException(NO_SUCH_RESOURCE,"[报修列表为空]");
        }
        log.debug("更新[投诉]列表缓存");
        repairRepository.setPageCache(pageNum,pageInfo);
        return pageInfo;
    }

    @Override
    public List<RepairType> loadRepairType() {
        List<RepairType> repairTypes = repairTypeMapper.listRepairType();
        return repairTypes;
    }

    @Override
    public void deleteRepair(Integer... ids) {
        int rows = repairMapper.deleteByIds(ids);
        if (rows==DELETE_FAULT){
            throw new SecurityException("未知错误");
        }
            log.debug("执行删除报修信息,执行重建缓存");
            loadCache();
    }

    @Override
    public void handleRepair(Integer id) {
        repairMapper.handleRepair(id);
        log.debug("执行处理报修信息,执行重建缓存");
        loadCache();
    }

    @Override
    public List<RepairVO> screenRepair(Integer repId, Integer status) {
        List<RepairVO> repairVOS = repairMapper.screenRepair(repId, status);
        return repairVOS;
    }


    @Override
    public void insert(RepairAddNewDTO repairAddNewDTO) {
        log.debug("前端传入添加的报修信息为:[{}]",repairAddNewDTO);
        String concurrentUserName = SecurityUtil.getConcurrentUserName();
        Repair repair = new Repair();
        UserVO concurrentUser = userMapper.findByUsername(concurrentUserName);
        repair.setRepId(repairAddNewDTO.getRepairId())
                .setRapDate(LocalDateTime.now())
                .setOwerId(concurrentUser.getId().intValue())
                .setStatue(0)
                .setClr(0)
                .setRemarks(repairAddNewDTO.getRemakes())
                .setUrls(repairAddNewDTO.getImgUrls());
        log.debug("根据报修值传递对象创建完整投诉对象[{}],并写入数据库",repair);
        int row = repairMapper.insert(repair);
        if (row!=HANDLE_SUCCESS){
            log.debug("返回受影响行数不为1,抛出异常");
            throw new ServiceException(UNKNOW_EXCEPTION,"服务器忙请稍后重试");
        }
    }

    @Override
    public List<RepairVO> loadPersonalRepair() {
        String concurrentUserName = SecurityUtil.getConcurrentUserName();
        log.debug("当前用户名为[{}]",concurrentUserName);
        List<RepairVO> list=repairMapper.listPersonalRepairVO(concurrentUserName);
        return list;
    }

    @Override
    public void initCache() {
        loadCache();
    }

    private void loadCache(){
        List<RepairVO> list = repairMapper.listAllRepairVO();
        repairRepository.clear();
        repairRepository.loadCache(list);
    }

}
